package com.lill.system.serivce.impl;

import com.lill.common.component.util.IdUtil;
import com.lill.common.component.util.StringUtils;
import com.lill.system.dao.ISysDictDao;
import com.lill.system.model.SysDictEntity;
import com.lill.system.model.SysUserEntity;
import com.lill.system.service.ISysDictService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统数据字典Service实现类
 *
 * @author lill
 */
@Service("dictService")
public class SysDictServiceImpl implements ISysDictService {
    @Autowired
    private ISysDictDao dictDao;

    public SysDictEntity queryObject(String id) {
        return dictDao.queryObject(id);
    }

    @Cacheable(value = "SysDictServiceImpl.queryListWithParams", keyGenerator = "cacheQueryKeyGenerator")
    public List<SysDictEntity> queryList(Map<String, Object> map) {
        return dictDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return dictDao.queryTotal(map);
    }

    public int save(SysDictEntity dict,SysUserEntity user) {
        dict.setId(IdUtil.createIdbyUUID());
        dict.setCreateTime(new Date());
        dict.setCreateUser(user.getId());
        return dictDao.save(dict);
    }

    public int update(SysDictEntity dict) {
        return dictDao.update(dict);
    }

    public int delete(String id) {
        return dictDao.delete(id);
    }

    public int deleteBatch(String[] ids) {
        return dictDao.deleteBatch(ids);
    }

    public Map<String, String> getDict(String groupCode,String reverse){
        Map<String, String> map = new HashMap<String, String>();
        map.put("groupCode",groupCode);
        List<SysDictEntity> list =  dictDao.queryList(map);
        Map<String, String> dict = new HashMap<String, String>();
        for(int i=0;i<list.size();i++){
            if("reverse".equals(reverse)){
                dict.put(list.get(i).getDictValue(),list.get(i).getDictKey());
            }else{
                dict.put(list.get(i).getDictKey(),list.get(i).getDictValue());
            }
        }
        return dict;
    }

	@Override
	public List<Map<String, Object>> queryAreaByPId(Map<String, Object> params) {
		return dictDao.findAreaByPId(params);
	}

	@Override
	public List<SysDictEntity> queryByCompany(Map<String, Object> params) {
		return dictDao.findDictByCompany(params);
	}

    @Override
    public List<SysDictEntity> queryDict(SysDictEntity dict) {
        return dictDao.queryDict(dict);
    }

	@Override
	public String queryObjByValOrKey(String name, String groupCode, SysUserEntity curUser) {
		Map<String, Object> mapParam=new HashMap<String, Object>();
		mapParam.put("keyVal", name);
		mapParam.put("groupCode", groupCode);
		int count=dictDao.countObjByValOrKey(mapParam);
		String key=name;
		if(count<=0){
			key=dictDao.findMaxKey(mapParam);
			if(!StringUtils.isNumber(key)){
				if(key.length()==1){
					if("Z".equals(key)){
						key="A1";
					}else{
						key=String.valueOf((char)(key.charAt(0)+1));
					}
				}else{
					String peff=key.substring(1,key.length());
					String suff=key.substring(1);
					if("Z".equals(peff)){
						key="A"+String.valueOf(Integer.parseInt(suff)+1);
					}else{
						key=String.valueOf((char)(peff.charAt(0)+1))+suff;
					}
				}
			}
			SysDictEntity dict=new SysDictEntity();
			dict.setDictKey(key);
			dict.setDictValue(name);
			dict.setGroupCode(groupCode);
			save(dict, curUser);
		}
		return key;
	}

}
